package com.jdk.demo.io.so_lingerDemo.demo2;

import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {

	public static void main(String[] args) throws Exception {
		ServerSocket serverSocket = new ServerSocket(8088);
		Socket socket = serverSocket.accept();
		System.out.println("A socket getSoLinger=" + socket.getSoLinger());
		socket.setSoLinger(false, 123123);
		System.out.println("B socket getSoLinger=" + socket.getSoLinger());

		OutputStream out = socket.getOutputStream();
		for (int i = 0; i < 10; i++) {
			out.write(("12345678901234567890123456789012345678901234567890"
					 + "12345678901234567890123456789012345678901234567890").getBytes());
		}
		out.write("end!".getBytes());
		System.out.println("socket close before=" + System.currentTimeMillis());
		out.close();
		socket.close();
		System.out.println("socket close after=" + System.currentTimeMillis());
		serverSocket.close();
		// 输出的before和end几乎一样，说明close没有阻塞
		// 客户端输出全部数据。从抓包结果分析，服务端向客户端一共传输了1004个字节，并且实现正常的4次挥手，和直接执行close的默认行为是一致的，都是将服务端发送缓冲区中的
		// 数据发送到客户端，然后执行4次挥手
	}

}
